package com.kenyi.co.ui.classification.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.util.Util;
import com.kenyi.co.GlideApp;
import com.kenyi.co.R;
import com.kenyi.co.ui.classification.bean.CategoryListBean;
import com.kenyi.co.ui.video.VideoDetailActivity;
import com.kenyi.co.utils.BaseMultiAdapter;
import com.kenyi.co.utils.LogU;
import com.kenyi.co.utils.SuperViewHolder;

import static com.kenyi.co.utils.CommonUtils.isFastClick;

/**
 * Created by Administrator on 2018/1/29.
 */


public class ClassificationRightAdapter extends BaseMultiAdapter<CategoryListBean.DatasBean.ArticlesBean> {


    public ClassificationRightAdapter(Context context) {
        super(context);
        addItemType(1, R.layout.fragment_right_item_3);
        addItemType(2, R.layout.fragment_right_item_1);
    }


    private void bindTextItem(SuperViewHolder holder, final CategoryListBean.DatasBean.ArticlesBean item) {

        final ImageView iv_item_vodeo = holder.getView(R.id.iv_item_vodeo);
        TextView tv_video_right_title = holder.getView(R.id.tv_video_right_title);
        TextView tv_video_right_sub_title = holder.getView(R.id.tv_video_right_sub_title);
        TextView tv_video_right_num = holder.getView(R.id.tv_video_right_num);
        TextView tv_video_right_time = holder.getView(R.id.tv_video_right_time);
        tv_video_right_time.setText(item.getVideoTime() + "");
        tv_video_right_title.setText(item.getTitle());
        tv_video_right_sub_title.setText(item.getDesc());
        tv_video_right_num.setText("播放次数：" + item.getReadCount() + "次");
        try {
            if (Util.isOnMainThread() && mContext != null) {
                GlideApp.with(mContext)
                        .load(item.getListImg())

                        .placeholder(R.drawable.iv_bg_video_right)
                        .error(R.drawable.iv_bg_video_right)
                        .into(iv_item_vodeo);
            }
        } catch (Exception e) {
        }

        LinearLayout li_home_content = holder.getView(R.id.li_home_content);
        li_home_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isFastClick()) {
                    return;
                }
                Intent metags = new Intent(mContext, VideoDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("id", item.getId() + "");
                metags.putExtras(bundle);
                mContext.startActivity(metags);
            }
        });

    }

    private void bindPicItem(SuperViewHolder holder, final CategoryListBean.DatasBean.ArticlesBean item, int position) {

        final ImageView iv_item_like = holder.getView(R.id.iv_item_like);
        TextView tv_video_title = holder.getView(R.id.tv_video_title);
        TextView tv_video_right_time = holder.getView(R.id.tv_video_right_time);
        View view_1 = holder.getView(R.id.view_1);
        View view_2 = holder.getView(R.id.view_2);
        if (position % 2 == 0) {
            view_1.setVisibility(View.GONE);
            view_2.setVisibility(View.VISIBLE);
        } else {
            view_1.setVisibility(View.VISIBLE);
            view_2.setVisibility(View.GONE);
        }

        RelativeLayout li_home_content = holder.getView(R.id.li_home_content);
        li_home_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isFastClick()) {
                    return;
                }
                Intent metags = new Intent(mContext, VideoDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("id", item.getId() + "");
                metags.putExtras(bundle);
                mContext.startActivity(metags);
            }
        });
        tv_video_right_time.setText(item.getVideoTime());
        tv_video_title.setText(item.getTitle());
        try {
            if (Util.isOnMainThread() && mContext != null) {
                GlideApp.with(mContext)
                        .load(item.getListImg())
                        .placeholder(R.drawable.iv_bg_video_right)
                        .error(R.drawable.iv_bg_video_right)
                        .into(iv_item_like);
            }
        } catch (Exception e) {
        }


    }


    @Override
    public void onBindItemHolder(SuperViewHolder holder, int position) {
        CategoryListBean.DatasBean.ArticlesBean item = mDataList.get(position);

        switch (item.getItemType()) {
            case 1:
                bindTextItem(holder, item);
                break;
            case 2:
                bindPicItem(holder, item, position);
                break;
            default:
                break;
        }

    }


}
